<script setup>
import { ref } from 'vue'
import useUserInfoStore from '@/stores/userinfo'
const UserInfoStore=useUserInfoStore();
const userPwd=ref({
    old_pwd:'',
    new_pwd:'',
    re_pwd:''
})
const checkRePassword=(rule,value,callback)=>{
    if(value===""){
        callback(new Error("请再次输入新密码"))
    }else if(value!==userPwd.value.new_pwd){
        callback(new Error("两次密码不一致"))
    }else{
        callback()
    }
}
const rules = {
    old_pwd: [
        { required: true, message: '请输入原密码', trigger: 'blur' },
        {
            pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    new_pwd: [
        { required: true, message: '请输入新密码', trigger: 'blur' },
        { pattern: /^\S{5,16}$/,
            message: '密码必须是5-16位的非空字符串',
            trigger: 'blur'
        }
    ],
    re_pwd: [
        {validator:checkRePassword,trigger:"blur"}
    ]
}
//修改密码
import { passwordUpdateService } from '@/api/user.js';
import { ElMessage } from 'element-plus';
const formRef=ref();
const updatePwd=async()=>{
    // 先进行表单验证
    const form = await formRef.value.validate();
    if (!form)
    ;
    // 验证失败则停止
    //调用接口
    let result=await passwordUpdateService(userPwd.value);
    ElMessage.success(result.message?result.message:"修改成功");
    //修改pinia里的值
    UserInfoStore.info.password=userPwd.value.new_pwd;
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>重置密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form ref="formRef" :model="userPwd" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="原密码" prop="old_pwd">
                        <el-input v-model="userPwd.old_pwd" type="password"></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input v-model="userPwd.new_pwd"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="re_pwd">
                        <el-input v-model="userPwd.re_pwd"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updatePwd">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>